import { defineStore } from 'pinia'
import { AppRouteRecordRaw } from '@/types/route'
import { MenuRoutesMap } from '@/types/login'
import { routesMap, notFoundRoute, homeRoute } from '@/router'
import { constantRoutes } from '@/router/constantRoutes'
import { filterPlatformName } from '@/utils/constantMap'

import accountRouter from '@/router/asyncRoutes/modules/account'

interface PermissionState {
  // 后台菜单列表
  routes: AppRouteRecordRaw[]
  // 菜单列表
  dynamicRoutes: AppRouteRecordRaw[]
  // name名称映射
  nameMap: any
}

export const permissionStore = defineStore('permission', {
  state: (): PermissionState => ({
    routes: [],
    dynamicRoutes: [],
    nameMap: {}
  }),
  actions: {
    generateRoutes(routesMapList: MenuRoutesMap[]) {
      let accessedRoutes = this.filterAsyncRoutes(routesMapList)
      this.nameMap = this.filterName(routesMapList)
      accessedRoutes.push(accountRouter)
      accessedRoutes.push(notFoundRoute)
      // 获取重定向的菜单
      let fullPath = accessedRoutes[0].path
      let children = accessedRoutes[0].children as AppRouteRecordRaw[]
      if (children && children.length) {
        if (filterPlatformName.includes(accessedRoutes[0].name)) {
          if (children[0].children && children[0].children.length) {
            fullPath = children[0].children[0].path
          }
        } else {
          fullPath = children[0].path
        }
      }

      homeRoute.redirect = fullPath
      accessedRoutes.unshift(homeRoute)
      this.routes = constantRoutes.concat(accessedRoutes)
      this.dynamicRoutes = accessedRoutes
    },
    filterName(routesMapList: MenuRoutesMap[]) {
      let nameMap: any = {}
      routesMapList.forEach((item: MenuRoutesMap) => {
        nameMap[item.functionUrl] = this.getNameArr(item.childList)
      })
      return nameMap
    },
    getNameArr(routesMapList: MenuRoutesMap[] | undefined) {
      let newChildren: any = []
      if (routesMapList && routesMapList.length) {
        for (const node of routesMapList) {
          newChildren.push(node.functionUrl)
          newChildren = [...newChildren, ...this.getNameArr(node.childList)]
        }
      }

      return newChildren
    },
    filterAsyncRoutes(routesMapList: MenuRoutesMap[]) {
      const res: AppRouteRecordRaw[] = []
      // 远程的>=本地的,且远程的覆盖本地的
      routesMapList.forEach((item: MenuRoutesMap) => {
        const localRoute = routesMap[item.functionUrl]
        if (localRoute || filterPlatformName.includes(item.functionUrl)) {
          let meta = {}
          if (!filterPlatformName.includes(item.functionUrl)) {
            const icon = item.icon || localRoute.meta.icon // 如果远程没配置，使用本地的icon
            meta = { ...localRoute.meta, icon, title: item.authorityName || localRoute.meta.title }
          }

          const tmp = {
            name: item.functionUrl,
            ...localRoute,
            meta,
            children: item.childList || []
          }
          if (tmp.children && tmp.children.length) {
            tmp.children = this.filterAsyncRoutes(tmp.children)
            if (filterPlatformName.includes(item.functionUrl)) {
              tmp.path = tmp.children[0].path
            }
          }
          res.push(tmp)
        }
      })
      return res
    }
  }
})
